package com.hileto.inf.utils.db;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 * Copyright: Copyright (c) 2012
 *     深圳市海乐淘电子商务有限公司
 * @Author : 曾林宝
 * @CreateTime : 2013-1-18 下午05:13:43
 *
 * @Description: 文件日志数据库系统.
 */
public class FileDB {
	
	private static LinkedList <Object> infoList = new LinkedList<Object>();
	protected static boolean isHasInfo = false;
	protected static boolean doShutdown = false;

	/**
	 * @Description : 保存对象.
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-18 下午05:14:15
	 * @param info
	 */
	public synchronized static void saveInfo(Object info){
		if(doShutdown){
			return ;
		}
		infoList.add(info);
		isHasInfo = true;
	}

	/**
	 * @Description : 复制一份数据去保存.不对外服务.
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-18 下午05:14:49
	 * @param tmpList
	 * @return
	 */
	protected synchronized static boolean dump(List<Object> tmpList) {
		boolean result = false;
		if(null == tmpList){
			tmpList = new ArrayList<Object>();
		}else if(!tmpList.isEmpty()){
			tmpList.clear();
		}
		Object obj = null;
		if(isHasInfo && !infoList.isEmpty()){
			while(null != (obj = infoList.poll())){
				tmpList.add(obj);
			}
			result = true;
		}
		isHasInfo = false;
		return result;
	}

	/**
	 * @Description : 关闭服务器.
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-18 下午05:15:39
	 */
	public synchronized static void shutdown() {
		doShutdown = true;
	}

	protected synchronized static boolean isShutdownThread() {
		if(!isHasInfo && doShutdown ){
			return true;
		}
		return false;
	}
}
